/*
Copyright (c) 2009-2010, Warwick Warp Limited
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the Warwick Warp Limited nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL WARWICK WARP LIMITED BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef WARRAY_WARRAY_POWER_H_INCLUDED
#define WARRAY_WARRAY_POWER_H_INCLUDED

#include "warray_vmath.h"

namespace warp {

//! \brief Square every element in an array
template<typename T,detail::warray_types::size_type N,typename Lptr>
void square(const const_arraynd<T,N,Lptr>& l, const arraynd<T,N>& r)
{
	detail::bounds_check(l,r);
	vm::square(l.size(),l.c_array(),r.c_array());
}

//! \brief Square every element in an array
template<typename T,detail::warray_types::size_type N,typename Lptr>
arraynd<T,N> square(const const_arraynd<T,N,Lptr>& l)
{
	arraynd<T,N> r(l.dims());
	warp::square(l,r);
	return r;
}

//! \brief Invert every element in an array
template<typename T,detail::warray_types::size_type N,typename Lptr>
void inv(const const_arraynd<T,N,Lptr>& l, const arraynd<T,N>& r)
{
	detail::bounds_check(l,r);
	vm::inv(l.size(),l.c_array(),r.c_array());
}

//! \brief Invert every element in an array
template<typename T,detail::warray_types::size_type N,typename Lptr>
arraynd<T,N> inv(const const_arraynd<T,N,Lptr>& l)
{
	arraynd<T,N> r(l.dims());
	warp::inv(l,r);
	return r;
}

//! \brief Square root every element in an array
template<typename T,detail::warray_types::size_type N,typename Lptr>
void sqrt(const const_arraynd<T,N,Lptr>& l, const arraynd<T,N>& r)
{
	detail::bounds_check(l,r);
	vm::sqrt(l.size(),l.c_array(),r.c_array());
}

//! \brief Square root every element in an array
template<typename T,detail::warray_types::size_type N,typename Lptr>
arraynd<T,N> sqrt(const const_arraynd<T,N,Lptr>& l)
{
	arraynd<T,N> r(l.dims());
	warp::sqrt(l,r);
	return r;
}

//! \brief Square root and invert every element in an array
template<typename T,detail::warray_types::size_type N,typename Lptr>
void invsqrt(const const_arraynd<T,N,Lptr>& l, const arraynd<T,N>& r)
{
	detail::bounds_check(l,r);
	vm::invsqrt(l.size(),l.c_array(),r.c_array());
}

//! \brief Square root and invert every element in an array
template<typename T,detail::warray_types::size_type N,typename Lptr>
arraynd<T,N> invsqrt(const const_arraynd<T,N,Lptr>& l)
{
	arraynd<T,N> r(l.dims());
	warp::invsqrt(l,r);
	return r;
}

//! \brief Raise every element in l to the power of the corresponding element in r
template<typename T,detail::warray_types::size_type N,typename Lptr,typename Rptr>
void pow(const const_arraynd<T,N,Lptr>& l, const const_arraynd<T,N,Rptr>& r, const arraynd<T,N>& o)
{
	detail::bounds_check(l,r,o);
	vm::pow(l.size(),l.c_array(),r.c_array(),o.c_array());
}

//! \brief Raise every element in l to the power of the corresponding element in r
template<typename T,detail::warray_types::size_type N,typename Lptr, typename Rptr>
arraynd<T,N> pow(const const_arraynd<T,N,Lptr>& l, const const_arraynd<T,N,Rptr>& r)
{
	arraynd<T,N> o(l.dims());
	warp::pow(l,r,o);
	return o;
}

//! \brief Raise every element in l to the power of r
template<typename T,detail::warray_types::size_type N,typename Lptr>
void pow(const const_arraynd<T,N,Lptr>& l, const T& r, const arraynd<T,N>& o)
{
	detail::bounds_check(l,o);
	vm::pow(l.size(),l.c_array(),r,o.c_array());
}

//! \brief Raise every element in l to the power of r
template<typename T,detail::warray_types::size_type N,typename Lptr>
arraynd<T,N> pow(const const_arraynd<T,N,Lptr>& l, const T& r)
{
	arraynd<T,N> o(l.dims());
	warp::pow(l,r,o);
	return o;
}

//! \brief Compute o_i = sqrt(l_i*l_i+r_i*r_i)
template<typename T,detail::warray_types::size_type N,typename Lptr,typename Rptr>
void hypot(const const_arraynd<T,N,Lptr>& l, const const_arraynd<T,N,Rptr>& r, const arraynd<T,N>& o)
{
	detail::bounds_check(l,r,o);
	vm::hypot(l.size(),l.c_array(),r.c_array(),o.c_array());
}

//! \brief Compute sqrt(l_i*l_i+r_i*r_i)
template<typename T,detail::warray_types::size_type N,typename Lptr,typename Rptr>
arraynd<T,N> hypot(const const_arraynd<T,N,Lptr>& l, const const_arraynd<T,N,Rptr>& r)
{
	arraynd<T,N> o(l.dims());
	warp::hypot(l,r,o);
	return o;
}


} // namespace warp

#endif
